#include <REGX52.H>
#include "LCD1602.h"
#include "matrixkey.h"
#include "Buzzer.h"
#include "Delay.h"
#include <math.h>
#define uchar unsigned char
#define uint unsigned int

// 密码系统相关变量
unsigned char KeyNum;
unsigned int Password1 = 0, Password2 = 0;
unsigned int secret1 = 11111;
unsigned int secret2 = 1;

unsigned char mode = 0;      // 0: 正常模式, 1: 修改密码模式, 2: 确认新密码模式
unsigned char loginAttempts = 3;
unsigned char lockFlag = 0;  // 锁定状态标志
unsigned char Min = 3, Sec = 0; // 锁定倒计时
unsigned int lockTime = 180; // 锁定时间 3 分钟
unsigned char password_count = 0; // 密码输入计数
unsigned int lockCount = 0; // 特此醒目立碑纪念它给我带来的黄金赤石体验

// 舵机相关变量
sbit pwm = P1^0;
uchar pwm_count = 0;
uchar n = 14; // 初始位置（90°）

// 延迟 5ms
void delay5ms() {
    unsigned char a, b;
    for (b = 19; b > 0; b--)
        for (a = 130; a > 0; a--);
}

// 初始化定时器 0，0.1ms 间隔
void InitTimer() {
    TMOD = 0x01;   // 设置定时器 0 为模式 1
    TH0 = 0xFF;    // 高位初值
    TL0 = 0x9C;    // 低位初值
    EA = 1;        // 总中断使能
    ET0 = 1;       // 定时器 0 中断使能
    TR0 = 1;       // 启动定时器 0
}

// 定时器中断服务函数（生成 PWM 信号）
void Timer() interrupt 1 {
    TH0 = 0xFF;
    TL0 = 0x9C;
    pwm_count++;
    if (pwm_count <= 200) {
        pwm = (pwm_count <= n) ? 1 : 0;
    } else {
        pwm_count = 0;
        pwm = 0;
    }
}

// 清除密码输入
void clearPassword() {
    Password1 = 0;
    Password2 = 0;
    password_count = 0;
    LCD_ShowString(2, 1, "------  ");
}

// 显示密码输入格式
void showPasswordFormat(unsigned char length) {
    unsigned char i;
    for (i = 0; i < length; i++) {
        if (i < password_count) {
            LCD_ShowChar(2, 1 + i, '*');
        } else {
            LCD_ShowChar(2, 1 + i, '-');
        }
    }
}

// 锁定状态倒计时
void startLockCountdown() {
    LCD_ShowString(1, 1, "Locked for 3min");
    LCD_ShowString(2, 1, " :");
    while (lockTime > 0) {
        LCD_ShowNum(2, 1, Min, 1); // 显示分钟
        LCD_ShowNum(2, 3, Sec, 2); // 显示秒数
        Delay(1000);               // 每秒延迟

        if (Sec > 0) {
            Sec--;
        } else {
            if (Min > 0) {
                Min--;
                Sec = 59;
            }
        }

        if (Min == 0 && Sec == 0) {
            lockFlag = 0; // 解除锁定
            LCD_ShowString(2, 1, "Time up!"); // 显示倒计时结束
            Delay(2000);
            LCD_ShowString(2, 1, "------  ");
            LCD_ShowString(1, 1, "PassWord:     ");
            break;
        }
    }
}

void main() {
    LCD_Init();
    LCD_ShowString(1, 1, "PassWord:");
    showPasswordFormat(6);
    InitTimer();

    while (1) {
        // 如果处于锁定状态，执行锁定倒计时
        if (lockFlag) {
            startLockCountdown();
            continue;
        }

        // 处理按键输入
        KeyNum = Matrixkey();
        if (KeyNum) {
            if (KeyNum <= 10) { // 数字按键
                if (password_count < 5) {
                    Password1 *= 10;
                    Password1 += KeyNum % 10;
                    password_count++;
                } else if (password_count == 5) {
                    Password2 = KeyNum % 10;
                    password_count++;
                }
            }
            showPasswordFormat(6);
        }

        if (KeyNum == 11) { // 确认键
            if (mode == 0) { // 正常模式
                if (Password1 == secret1 && Password2 == secret2) {
                    clearPassword();
                    LCD_ShowString(1, 1, "Successfully    ");
                    LCD_ShowString(2, 1, "Log  in!        ");
                    P2_1 = 0; // 绿灯亮
                    loginAttempts = 3; // 重置登录尝试次数

                    // 设置舵机角度为 90°
                    n = 6;
                    Delay(2000); // 保持位置 2 秒
                    n = 14;
                } else {
                    loginAttempts--;
                    if (loginAttempts > 0) {
                        LCD_ShowString(1, 1, "ERROR");
                        LCD_ShowString(2, 1, "        TryAgain");
                        clearPassword();
                        P2_1 = 1; // 绿灯灭
                        P2_0 = 0; // 红灯亮
                        Delay(1000);
                        P2_0 = 1; // 红灯灭
                        Buzzer_Time(1000);
                        LCD_ShowNum(1, 14, loginAttempts, 1);
                    } else {
                        LCD_ShowString(1, 1, "ERROR");
                        LCD_ShowString(2, 1, "Log in Disabled ");
                        loginAttempts = 3;
                        lockFlag = 1;
                        Delay(1000);
                    }
                }
            }
        }

        if (KeyNum == 12) { // 退位键
            if (password_count > 0) {
                if (password_count <= 5) {
                    Password1 /= 10;
                } else {
                    Password2 = 0;
                }
                password_count--;
                showPasswordFormat(6);
            }
        }
    }
}
